﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;

/// <summary>
/// Summary description for ShoppingOnlineCart
/// </summary>
public class ShoppingOnlineCart
{
    private string idPro;
    private string namePro;
    private string imagePro;
    private int luongPro;
    private int pricePro;
    private int sumPro;
    public ShoppingOnlineCart(string _ma, string _ten, string _image, int _luong, int _gia)
    {
        this.idPro = _ma;
        this.namePro = _ten;
        this.imagePro = _image;
        this.luongPro = _luong;
        this.pricePro = _gia;
    }
    public string IdPro
    {
        set { idPro = value; }
        get { return idPro; }
    }
    public string NamePro
    {
        set { namePro = value; }
        get { return namePro; }
    }
    public string ImagePro
    {
        set { imagePro = value; }
        get { return imagePro; }
    }

    public int LuongPro
    {
        set { luongPro = value; }
        get { return luongPro; }
    }

    public int PricePro
    {
        set { pricePro = value; }
        get { return pricePro; }
    }

    public int SumPro
    {
        set { sumPro = value; }
        get { return (PricePro * LuongPro); }
    }
}
public class CartOnline
{
    private Hashtable _table = new Hashtable();
    private int _tong = 0;
    public void Add(string id, string img, string name, int gia)
    {
        // Nếu sản phẩm không có trong giỏ hàng thì thêm hàng vào giỏ
        if (_table[id] == null)
        {
            ShoppingOnlineCart hd = new ShoppingOnlineCart(id, name, img, 1, gia);
            _table.Add(id, hd);
            _tong += hd.SumPro;
        }

         //Nếu đã có sản phẩm trong giỏ thì cập nhật thêm sản phẩm
        else
        {
            ShoppingOnlineCart hd = (ShoppingOnlineCart)_table[id];
            hd.LuongPro += 1;
            hd.SumPro += hd.PricePro;
            _tong += hd.PricePro;
        }
    }
    public void Remove(string id)
    {
        if (_table[id] != null)
        {
            ShoppingOnlineCart row = (ShoppingOnlineCart)_table[id];
            _tong -= row.SumPro;
            _table.Remove(id);
        }
    }
    public void Update(string id, int newQty)
    {
        if (_table[id] != null)
        {
            ShoppingOnlineCart row = (ShoppingOnlineCart)_table[id];
            _tong -= row.SumPro;
            row.LuongPro = newQty;
            row.SumPro = (row.LuongPro * row.PricePro);
            _tong += row.SumPro;
        }
    }
    public ICollection Table
    {
        get { return _table.Values; }
    }
    public int TotalPrice
    {
        get { return _tong; }
    }
}